 <template>
	 <view>
		 <view class="author">
			 <view class="author-img">
				 <u-avatar :src="avatar" size="60" ></u-avatar>
			 </view>
			<view style="margin: 10px;">
				<view class="author-infomation" style="font-size: 20px; font-weight: 500;">{{nickname}}</view>
				<view style="font-size: 12px; margin-top: 25px;">账号：{{account}}</view>
			</view>
		 </view>
		<u-cell-group>
			<u-cell icon="account-fill" title="登录" :arrow="true" arrow-direction="down"  @click="denglu">{{nickname}}</u-cell>
			<u-cell icon="account-fill" title="人脸登录" :arrow="true" arrow-direction="down"  @click="facedenglu"></u-cell>
			<u-cell icon="plus-people-fill" title="注册" :arrow="true" arrow-direction="down" @click="register"></u-cell>
			<u-cell icon="minus-people-fill" title="退出" :arrow="false" @click="out()"></u-cell>
		</u-cell-group>
		<!-- 密码登录 -->
		<u-popup :show="showT" mode="top" width="500rpx" height="600px" :closeable="true" @close="this.showT=false;this.nickname='未登录';this.account='未登录'" :round="10">
			<view>
				<h1 style="text-align: center">账号登录</h1>
				<view>账号：<u-input  :type="text" :border="true" v-model="account" /></view>
				<view>密码：<u-input  :type="password" :border="true"  :password-icon="true"  v-model="password"/></view>
				<u-button type="primary" shape="square" @click="goto(account,password)">登录</u-button>
			</view>
		</u-popup>
		<!-- face登录 -->
		<u-popup :show="faceT" mode="center" :closeable="true" @close="this.faceT=false">
			<view v-if="faceT">
				<camera id="faceT" device-position="front" flash="off" style="width: 300px; height: 300px;" frame-size="medium"></camera>
				<u-button type="primary" shape="square" @click="face()">登录</u-button>
			</view>
		</u-popup>
		<!-- 注册 -->
		<u-popup :show="checkT" mode="top" width="500rpx" height="600px" :closeable="true" @close="this.checkT=false;this.nickname='未登录';this.account='未登录'" :round="10">
			<view>
				<h1 style="text-align: center">账号注册</h1>
				<view>昵称：<u-input  :type="text" :border="true" v-model="nickname" /></view>
				<view>账号：<u-input  :type="text" :border="true" v-model="account" /></view>
				<view>密码：<u-input  :type="password" :border="true"  :password-icon="true"  v-model="password"/></view>
				<u-button type="primary" shape="square" @click="gotoinfo(nickname,account,password)">注册</u-button>
			</view>
		</u-popup>
	</view>
</template>



<script>
	export default {
		data() {
			return {
				// account: '',
				password: '',
				account:'未登录',
				nickname:'未登录',
				avatar:'/static/c1.png',
				checkT: false,//注册
				showT : false,//登录
				faceT : false,//face登录
				image:false,//照片更改
				fileList1:[]
		    }
		},
		onLoad() {
		},
		methods: {
			async gotoinfo(nickname,account,password){
				if(nickname===""||account===""||password===""){
					return uni.$showMsg("请输入完整",3000)
				}
				console.log(nickname,account,password);
				const {data:res}=await uni.$http.post("/register",{
					nickname,
					account,
					password
					// data:userForm
					// data: JSON.stringify(userForm), // 将userForm对象转换为JSON字符串  
				});
				console.log(res);
				if(res.code!==200){
					return uni.$showMsg()
				}else{
					this.checkT=false
					this.showT=true
				}
				// uni.switchTab({
				// 	url : '/pages/Logon1/Logon1'
				// });
			},
		    async out(){
				const {data:res}=await uni.$http.get("/logout")
				console.log(res);
				if(res.code!==200){
					return uni.$showMsg()
				}else{
					if(uni.getStorageSync('id')!==""){
						// console.log(uni.getStorageSync('id'));
						uni.setStorageSync('token','')
						uni.setStorageSync('id','')
						this.account='未登录'
						this.nickname='未登录'
						this.avatar='/static/c1.png'
						return uni.showToast({
								title:'退出成功',
								duration:1000,
								icon:'none'
							})
					}else{
						return uni.showToast({
								title:'请先登录',
								duration:3000,
								icon:'none'
							})
					}
					
				}
				// 可以将放在缓存中的token设定为空
				console.log(res);
			},
			async getuser(){
				const {data : ros} = await uni.$http.get("/users/currentUser");
				console.log(ros);
				this.account=ros.data.account
				this.nickname=ros.data.nickname
				this.avatar=ros.data.avatar
				// 放出登录者id
				uni.setStorageSync('id',ros.data.id)
				// uni.$emit('id',{id : ros.data.id})
				console.log(ros);
			},
			async face(){
				const that=this;
				const context=uni.createCameraContext();
				const photo=context.takePhoto({
					async success(res) {
						console.log(res);
						const tempFilePaths = res.tempImagePath
						console.log(tempFilePaths);
						    // 接下来我们要读取这个文件，并转换为 Base64 编码的字符串  
						    const data= wx.getFileSystemManager().readFile({  
						      filePath: tempFilePaths,  
						      encoding: 'base64',  
						      success: async (res) => {  
						        const base64Str = res.data  
						        // console.log(base64Str) // 这就是你的 Base64 编码的字符串  
								const {data:ris} =await uni.$http.post('/faceAuth/faceSearch',{
									base64Str
									})
								if(ris.code!==200) return uni.$showMsg("人脸登录失败，请重新开始",1500)
								console.log(ris.data);
								uni.setStorageSync('token',ris.data)
								that.faceT=false;
								that.getuser();
								return uni.$showMsg("人脸登录成功",3500)
						      },  
						      fail: (err) => {  
						        console.error(err)  
						      }  
						      });
					},
					fail() {
						console.log("失败");
					}
				})
			},
			async goto(account,password){
				// console.log(account,password);
				const {data : res}=await uni.$http.post("/login",{
					account,
					password
				});
				console.log(res);
				if(res.code!==200){
					return uni.$showMsg("账号密码错误",1500)
				}else{
					this.data=res.data
					// console.log(this.data);
					// 将this.data存入缓存中
					uni.setStorageSync('token',this.data)
					this.showT=false
					this.getuser()
				}
			},
			denglu(){
				if(uni.getStorageSync("token")!==""){
					return uni.$showMsg("请退出登录",2000)
				}
				this.showT=true
				this.account=""
				this.nickname=""
				this.password=''
			},
			facedenglu(){
				if(uni.getStorageSync("token")!==""){
					return uni.$showMsg("请退出登录",2000)
				}
				this.faceT=true
				
			},
			register(){
				if(uni.getStorageSync("token")!==""){
					return uni.$showMsg("请退出登录",2000)
				}
				this.checkT=true
				this.account=""
				this.nickname=""
				this.password=""
			}
		}
	}
</script>

<style  lang="scss">
.author{
	display: flex;
	position: relative;
	.author-img{
		margin: 10px;
	}
}
</style>
